package inline;

import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;

import com.bytedance.frameworks.core.encrypt.TTEncryptUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class UrlDemo {
    public static com.bytedance.frameworks.core.encrypt.a a;

    public static String a(String arg2, String arg3) {
        if (arg3 == null) {
            arg3 = "ISO-8859-1";

        }
        try {
            return URLEncoder.encode(arg2, arg3);
        } catch (UnsupportedEncodingException v0) {
            throw new IllegalArgumentException(((Throwable) v0));
        }
    }

    public static String a(List arg5, boolean arg6, String arg7) {
        String v0_1;
        String v2;
        StringBuilder v3 = new StringBuilder();
        Iterator v4 = arg5.iterator();
        while (v4.hasNext()) {
            Pair<String, String> v0 = (Pair<String, String>) v4.next();
            String v1 = v0.first;
            if (arg6) {
                v2 = UrlDemo.a(v0.first, arg7);
            } else {
                v2 = v1;
            }

            v1 = v0.second;
            if (!arg6) {
                v0_1 = v1;
            } else if (v0.second != null) {
                v0_1 = UrlDemo.a(v0.second, arg7);
            } else {
                v0_1 = "";
            }

            if (v3.length() > 0) {
                v3.append("&");
            }

            v3.append(v2);
            v3.append("=");
            v3.append(v0_1);
        }

        return v3.toString();
    }

    public static void parseUrl(URI uri, Map arg8) throws IOException {
        int v0 = 0;
        if (uri == null) {
            throw new IOException("parseUrl url is null !!!");
        }

        try {
            String v1 = uri.getRawQuery();
            if (v1 != null) {
                String[] v1_1 = v1.split("&");
                int v2 = v1_1.length;
                while (v0 < v2) {
                    String v3 = v1_1[v0];
                    int v4 = v3.indexOf("=");
                    if (v4 >= 0) {
                        arg8.put(URLDecoder.decode(v3.substring(0, v4), "UTF-8"), URLDecoder.decode(v3.substring(v4 + 1), "UTF-8"));
                    } else {
                        arg8.put(URLDecoder.decode(v3, "UTF-8"), "");
                    }

                    ++v0;
                }
            }
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
            throw new IOException("parseUrl url is null !!!");
        }
    }

    private static boolean a(Map arg2, List arg3, String arg4) throws UnsupportedEncodingException {
        boolean v0 = false;
        if (!TextUtils.isEmpty(((CharSequence) arg4)) && (arg2.containsKey(arg4))) {
            arg3.add(new Pair(arg4, arg2.get(arg4)));
            if (!a.d()) {
                arg2.remove(arg4);
            }

            v0 = true;
        }

        return v0;
    }


    public static String a(String arg7, List arg8) {
        if (!TextUtils.isEmpty(((CharSequence) arg7)) && a != null) {
            LinkedHashMap v0 = new LinkedHashMap();
            try {
                URI v1 = URI.create(arg7);
                UrlDemo.parseUrl(v1, ((Map) v0));
                LinkedList v2 = new LinkedList();
                UrlDemo.a(((Map) v0), ((List) v2), "device_id");
                UrlDemo.a(((Map) v0), ((List) v2), "device_type");
                UrlDemo.a(((Map) v0), ((List) v2), "device_brand");
                UrlDemo.a(((Map) v0), ((List) v2), "uuid");
                UrlDemo.a(((Map) v0), ((List) v2), "openudid");
                String v2_1 = UrlDemo.a(((List) v2), false, "UTF-8");
                if (TextUtils.isEmpty(((CharSequence) v2_1))) {
                    return arg7;
                }

                byte[] v2_2 = v2_1.getBytes();
                v2_2 = TTEncryptUtils.encryptBytes(v2_2, v2_2.length);
                if (v2_2 == null) {
                    return arg7;
                }

                v2_1 = Base64.encodeToString(v2_2, 2);
                LinkedList v3 = new LinkedList();
                if (a.b()) {
                    ((List) v3).add(new Pair("ss_queries", v2_1));
                }

                if (a.c()) {
                    arg8.add(new Pair("X-SS-QUERIES", UrlDemo.a(v2_1, "UTF-8")));
                }

                if (!((Map) v0).isEmpty()) {
                    Iterator v2_3 = ((Map) v0).entrySet().iterator();
                    while (v2_3.hasNext()) {
                        Object v0_2 = v2_3.next();
                        if (v0_2 == null) {
                            continue;
                        }

                        ((List) v3).add(new Pair(((Map.Entry) v0_2).getKey(), ((Map.Entry) v0_2).getValue()));
                    }
                }

                String v0_3 = UrlDemo.a(((List) v3), true, "UTF-8");
                v2_1 = v1.getScheme();
                String v3_1 = v1.getHost();
                int v4 = v1.getPort();
                String v5 = v1.getRawPath();
                String v1_1 = v1.getRawFragment();
                StringBuilder v6 = new StringBuilder();
                if (v3_1 != null) {
                    if (v2_1 != null) {
                        v6.append(v2_1);
                        v6.append("://");
                    }

                    v6.append(v3_1);
                    if (v4 <= 0) {
                    } else {
                        v6.append(':');
                        v6.append(v4);
                    }
                }
                if (v5 == null || !v5.startsWith("/")) {
                    v6.append('/');
                }

                if (v5 != null) {
                    v6.append(v5);
                }

                if (v0_3 != null) {
                    v6.append('?');
                    v6.append(v0_3);
                }

                if (v1_1 != null) {
                    v6.append('#');
                    v6.append(v1_1);
                }

                arg7 = new URI(v6.toString()).toString();
            } catch (Throwable v0_1) {
                v0_1.printStackTrace();
            }
        }

        return arg7;
    }
}
